package com.ruoyi.core.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.core.domain.custombean.MaintenanceInfoEntryResult;
import com.ruoyi.core.service.MaintenanceInfoService;
import com.ruoyi.core.vo.MaintenanceInfoCreateVO;
import com.ruoyi.core.vo.MaintenanceInfoQRFileCreateVO;
import com.ruoyi.core.vo.MaintenanceInfoUpdateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api("客户端维护信息管理")
@RestController
@RequestMapping("/maintenance-infos")
public class MaintenanceInfoController extends BaseController {

    @Autowired
    private MaintenanceInfoService maintenanceInfoService;

    /**
     * 新增client_product
     */
    @ApiOperation("维护信息新增")
    @PreAuthorize("@ss.hasRole('admin')")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody MaintenanceInfoCreateVO maintenanceInfoCreateVO) {
        try {
            return AjaxResult.success((maintenanceInfoService.add(maintenanceInfoCreateVO)));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增client_product
     */
    @ApiOperation("维护信息文件新增")
    @PreAuthorize("@ss.hasRole('admin')")
    @PostMapping("/file")
    public AjaxResult addFile(@Valid @ModelAttribute MaintenanceInfoQRFileCreateVO maintenanceInfoQRFileCreateVO) {
        try {
            return AjaxResult.success((maintenanceInfoService.addFile(maintenanceInfoQRFileCreateVO)));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增client_product
     */
    @ApiOperation("维护信息文件更新")
    @PreAuthorize("@ss.hasRole('admin')")
    @PutMapping("/file")
    public AjaxResult updateFile(@Valid @ModelAttribute MaintenanceInfoQRFileCreateVO maintenanceInfoQRFileCreateVO) {
        try {
            return AjaxResult.success((maintenanceInfoService.updateFile(maintenanceInfoQRFileCreateVO)));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增client_product
     */
    @ApiOperation("维护信息更新")
    @PreAuthorize("@ss.hasRole('admin')")
    @PutMapping
    public AjaxResult update(@Valid @RequestBody MaintenanceInfoUpdateVO maintenanceInfoUpdateVO) {
        try {
            return AjaxResult.success((maintenanceInfoService.update(maintenanceInfoUpdateVO)));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @ApiOperation("维护信息查询")
    @GetMapping
    public AjaxResult list() {
        List<MaintenanceInfoEntryResult> list = maintenanceInfoService.findByKeyLike("sys");
        return AjaxResult.success(list);
    }

    @ApiOperation("维护信息单项查询")
    @GetMapping("/{key}")
    public AjaxResult findByKey(@PathVariable("key") String key) {
        return AjaxResult.success(maintenanceInfoService.findByKey(key));
    }

    @ApiOperation("维护信息单项查询")
    @GetMapping("/getSaleInfo")
    public AjaxResult getSaleInfo() {
        return AjaxResult.success(maintenanceInfoService.findByKey("sys_merchant_web_sale_qr"));
    }

}
